package ins.yan.framework.service.dao.common;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class GeneralDaoSupport<T> extends HibernateDaoSupport{

	private Class<T> entityClass;

	/**
	 * 这个通常也是hibernate的取得子类class的方法
	 * 
	 * @author "yangk"
	 * @date 2010-4-11 下午01:51:28
	 */
	public GeneralDaoSupport() {
		Type genType = getClass().getGenericSuperclass();
		Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
		entityClass = (Class) params[0];
	}

	public T get(String id) {
		try {
			System.out.println("HibernateBaseDao:"+entityClass.getSimpleName());
			return entityClass.newInstance();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	
	public Object findMaxId() {
		String hql = "select max(id) from " + entityClass.getSimpleName();
		int maxId;
		try {
			maxId = (Integer)super.getHibernateTemplate().find(hql).get(0);
		} catch (NullPointerException e) {
			maxId = 0;
		}
		return maxId;
	}

	public void saveEntity(T entity) {
		super.getHibernateTemplate().save(entity);
	}

}